package com.carsa.blackberry.ui.screen.common;

import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.FieldChangeListener;
import net.rim.device.api.ui.Font;
import net.rim.device.api.ui.Graphics;
import net.rim.device.api.ui.Manager;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.component.ButtonField;
import net.rim.device.api.ui.component.Dialog;
import net.rim.device.api.ui.container.MainScreen;
import net.rim.device.api.ui.container.VerticalFieldManager;

import com.carsa.blackberry.MainKeys;
import com.carsa.blackberry.ui.UIKeys;
import com.carsa.blackberry.util.Logger;
import com.carsa.blackberry.util.Util;

/**
 * @author Nahuel Barrios.
 * @since 20/07/2010.
 * @version 1.0
 */
public class BasicScreen extends MainScreen {

    protected final MainScreen     _currentScreen = this;
    protected VerticalFieldManager _mainManager;
    protected ButtonField          _bf_accept;

    public BasicScreen() {
	super();
	this.customizeScreen();
    }

    public BasicScreen(String screenTitle) {

	if (screenTitle != null)
	    this.setTitle(screenTitle);
    }
    
    protected boolean keyDown(int arg0, int arg1) {
	switch (arg0) {
        case MainKeys.DROP_CALL_KEY_CODE:
	    if (Util.getBooleanConditionFromDialogResponse(Dialog.ask(Dialog.D_YES_NO, UIKeys.POPUP_CONFIRMAR_OPERACION)))
		System.exit(0);
	    return true;	    
        case MainKeys.ANSWER_CALL_KEY_CODE:
            return false;
        case MainKeys.LEFT_BUTTON_KEY_CODE:
            return false;
        case MainKeys.RIGHT_BUTTON_KEY_CODE:
            return false;
        default:
	    break;
        }
	
	return super.keyDown(arg0, arg1);
    }
       
    public boolean onClose() {
	UiApplication.getUiApplication().popScreen(_currentScreen);
	return true;
    }

    /**
     * Setea las caracteristicas de visualizacion como la fuente, los colores, el fondo, etc.
     */
    public void customizeScreen() {

	// Seteo el color de fondo en el Manager principal en el que tenemos que agregar todos los controles.
	_mainManager = new VerticalFieldManager(FIELD_HCENTER) {
	    protected void paintBackground(Graphics graphics) {
		graphics.setBackgroundColor(UIKeys.COLOR_APP_BACKGROUND);

		graphics.clear();
		super.paint(graphics);
	    }
	};

	// Seteo la fuente que se usara en toda la aplicacion.
	this.setFont(this.getFont().getFontFamily().getFont(Font.PLAIN, UIKeys.SIZE_FOR_APPLICATION_FONT));
    }

    /**
     * Realiza las acciones necesarias antes de dejar la pantalla.
     * 
     * @return
     */
    protected boolean doBeforeLeaveScreen() {
	return true;
    }

    /**
     * Agrega un boton de volver a la screen aScreen que le pasamos como parametro.
     * 
     * @param aScreen
     * @return
     */
    public boolean addBackButton(Manager parent) {
	if (parent == null)
	    return false;

	ButtonField bf_back = new ButtonField(UIKeys.TITLE_VOLVER, Field.FIELD_RIGHT);
	bf_back.setChangeListener(new FieldChangeListener() {
	    public void fieldChanged(Field field, int context) {
		onClose();
	    }
	});

	parent.add(bf_back);
	return true;
    }

    // /**
    // * Agrega un boton de volver a la screen actual.
    // *
    // * @return
    // */
    // public boolean addAcceptButton() {
    // if (_nextScreen==null)
    // return false;
    //
    // _bf_accept = new ButtonField(MainKeys.TITLE_ACCEPT,
    // Field.FIELD_RIGHT);
    //
    // return true;
    // }

    // public void addBottomToolbar(boolean acceptButton, boolean backButton){
    // if(acceptButton)
    // this.addAcceptButton();
    //
    // if(backButton)
    // this.addBackButton();
    //		
    // this.add(_bottomToolbar);
    // }

}
